home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 577 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  91 lines

  1. Path: news.magic.mb.ca!not-for-mail
  2. From: wrightd@merlin.magic.mb.ca (David C. Wright)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: ugly constants and header files
  5. Date: 6 Jan 1996 23:55:01 -0600
  6. Organization: Magic Online Services Winnipeg.
  7. Message-ID: <k607w0zpkP3U088yn@merlin.magic.mb.ca>
  8. References: <1996Jan3.155754.111142@kuhub.cc.ukans.edu>
  9. Reply-To: wrightd@merlin.magic.mb.ca
  10. NNTP-Posting-Host: merlin.magic.mb.ca
  11. X-Newsreader: Yarn 0.88 with YES 0.20
  12. X-Info: YES is the Yarn Editor Shell, a freeware utility for Yarn!
  13.         The best supplement to the best offline newsreader!
  14.  
  15. In group comp.lang.c, article <1996Jan3.155754.111142@kuhub.cc.ukans.edu>,
  16. anh@kuhub.cc.ukans.edu telepathically conveyed:
  17. >Hello,
  18. >
  19. >I have a list of constants, some defined using #define, some are string 
  20. >constants a la char *gSuperStrings[]={"aa","bb"};
  21. >
  22. >These constants are used by multiple modules which are linked together to 
  23. >a program. However, I have more than one program using these constants,
  24. >hence I put the constants in a header file. I know it is ugly to put
  25. >definitions in a header file.
  26. >
  27. >My current solution is to change all the #define to integer constants and 
  28. >to keep 2 files:
  29. >
  30. >my_const.h        --- To be included only once (perferably by the main() 
  31. >              module). Definitions
  32. >
  33. >my_ext.h          --- To be included by any modules using the constants.
  34. >                      Declarations.
  35. >
  36. >Is this the best solution? Are there any other way?
  37.  
  38. Create a single header file, of the form:
  39.  
  40. [begin file]
  41. /* whatever */
  42.  
  43. #define CONSTANT 320
  44.  
  45. struct anystruct
  46. {
  47.   int onevar;
  48.   int twovar;
  49. };
  50. typedef struct anystruct anystruct_t;
  51.  
  52. enum enumvars = {one, two, three};
  53. typedef enum enumvars enumvars_t;
  54.  
  55. #ifdef GLOBALS
  56.  
  57. int globalvar;
  58. anystruct_t *ptr1;
  59. enumvars_t evars;
  60.  
  61. #else
  62.  
  63. extern int globalvar;
  64. extern anystruct_t *ptr1;
  65. extern enumvars_t evars;
  66.  
  67. #endif
  68.  
  69. int anyfunction(void);
  70.  
  71. /* whatever */
  72. [end file]
  73.  
  74. #include this file in any modules that use the #defines/variables/prototypes
  75. in question.  In *one* of the modules, use;
  76.  
  77. #define GLOBALS
  78.  
  79. *before* the #include statement that includes your header file.  The net
  80. effect is that the variables/structures/whatever will be declared once in
  81. the module that has the #define statement in it, and, in all the others, it
  82. will be declared as an external, which will satisfy the linker.  All the
  83. definitions, such as structures, typedefs, #defines, prototypes, etc will
  84. be defined in every module.  Everything will be happy.
  85.  
  86. -- 
  87.            David C. Wright - wrightd@merlin.magic.mb.ca
  88.    E-mail me for my PGP public key.  My public key changed on Dec 5, 1995.
  89.  
  90.          And God said: E = *mv* - Ze*/r ...and there *WAS* light!
  91.